#ifndef  _SDLMGR_H__
#define  _SDLMGR_H__

#include "global.h"
#include "SDL.h"
#include "SDL_ttf.h"
#include <string>
#include <queue>

struct SAudioData
{
    char *pBuf;
    int  nBufLen;
    double pts;
};

struct SVideoData
{
    uint8_t *pYUV;
    int  nYLen;
    int  nULen;
    int  nVLen;
    double pts;
};

class CSDLMgr
{
public:
    CSDLMgr();
    ~CSDLMgr();

    int Init(int nW,int nH,int nFps,int64_t nTimeLen);

    bool NeedExit();

    //play button
    bool NeedPlayCtrl();

    int InputVideoData(uint8_t *pY,int nYLen,uint8_t *pU,int nULen,uint8_t *pV,int nVLen, double pts);

    void			  InitAudio(int out_sample_rate,int out_channels,int nFrameSize);
    void			  InputAudioData(unsigned char *pData, int nLen, double pts);
    static void     fill_audio(void *udata,Uint8 *stream,int len);
private:
    SDL_Texture *LoadTexture(SDL_Renderer *renderer, const char *file, SDL_bool transparent);
    void ApplySurface(int x, int y, SDL_Texture *tex, SDL_Renderer *rend);
    SDL_Texture* LoadImage(std::string file);
    void		      FillMyAudio(Uint8 *stream,int len);
    int DrawYUV(uint8_t *pY,int nYLen,uint8_t *pU,int nULen,uint8_t *pV,int nVLen, double pts);
    void ClearAV();
    void DrawButton(int64_t pts);
    int DrawText(int x,int y, const char* text);
    void ToTimeStr(int64_t nTime,std::string &strTime);

private:
    SDL_Window     *m_pSdlWindow;
    SDL_Texture    *m_pSdlTexture;
    SDL_Rect        m_SdlRect;
    SDL_Renderer   *m_pRenderer;
    SDL_Renderer   *m_pRenderer2;
    SDL_Texture    *m_pTexturePlayTime;

    int             m_nFrameW;
    int             m_nFrameH;

    TTF_Font        *m_pFont;

    //button
    SDL_Rect m_rectPlayPauseBtn;
    SDL_Rect m_rectBackBtn;
    SDL_Rect m_rectRotateBtn;
    SDL_Rect m_rectProgressBtn;

    SDL_Texture     *m_pBtnUpTexture ;
    SDL_Texture     *m_pBtnDownTexture ;
    SDL_Texture     *m_pBtnBackTexture ;
    SDL_Texture     *m_pBtnProgressBar;
    SDL_Texture     *m_pBtnCursorTexture;
    SDL_Texture     *m_pBtnRotateTexture;

    enPlayState          m_enOldPlayBtn;
    enPlayState          m_enPlayBtn;

    //设备分辨率
    int                 m_nDevW;
    int                 m_nDevH;
    int                 m_nFps;
    int64_t             m_nTimeLen;
    bool                m_bRorate;//旋转

    //audio
    Uint8			*m_pAudioData;
    std::queue<SAudioData>   m_queAudioData;
    std::queue<SVideoData>   m_queVideoData;
    double          m_dbCurPts;

};

#endif